import { _decorator, Color, Component, EventTouch, Node, Prefab, tween, v3, Vec2 } from 'cc';
import { PoolManager } from './PoolManager';
import { CircleRing } from './CircleRing';
import { CirclePlayer } from './CirclePlayer';
import { CIRCLE_ROMOVE } from './Constant';
const { ccclass, property } = _decorator;

@ccclass('CircleRingManager')
export class CircleRingManager extends Component {
    public static Instance: CircleRingManager = null;

    @property(Node)
    Player: Node = null;

    @property(Prefab)
    ItemPrefab: Prefab = null;

    @property
    Radius: number = 600;

    @property
    Width: number = 40;

    @property
    SubRadius: number = 50;

    @property(Node)
    AredNode: Node = null;

    @property(Node)
    StartNode: Node = null;


    public Color: Color = new Color();
    public CurRadius: number = 0;
    public CurItem: Node = null;
    public IsStart: boolean = false;

    private _maxCircle: number = 50;
    private _curCircle: number = 0;
    private _touchStart: Vec2 = new Vec2();

    protected onLoad(): void {
        CircleRingManager.Instance = this;

        PoolManager.Instance.preload(this.ItemPrefab, this._maxCircle);
        this.AredNode.once(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.AredNode.once(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.AredNode.once(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
        tween(this.StartNode)
            .to(1, { scale: v3(1.2, 1.2, 1.2) }, { easing: `sineIn` })
            .to(1, { scale: v3(1, 1, 1) }, { easing: `sineIn` })
            .union()
            .repeatForever()
            .start();
    }

    start() {
        this.createItem();
    }

    createItem() {
        this._curCircle++;
        this.CurItem = PoolManager.Instance.get(this.ItemPrefab);
        this.Color = this.getRandomBrightColor();
        this.CurRadius = this.Radius;
        this.CurItem.getComponent(CircleRing).init(this.node, this.CurRadius, this.Width, this.Color);
    }

    crash(): CIRCLE_ROMOVE {
        if (this._curCircle > this._maxCircle) return CIRCLE_ROMOVE.IDLE;

        this.CurRadius -= this.SubRadius;
        if (this.CurRadius < this.Player.getComponent(CirclePlayer).curRadius) {
            this.Player.getComponent(CirclePlayer).addCircle(this.Player.getComponent(CirclePlayer).curRadius, this.Width, this.Color);
            this.CurItem.getComponent(CircleRing).clear();
            PoolManager.Instance.put(this.CurItem);
            this.createItem();
            return this.Radius > this.Player.getComponent(CirclePlayer).curRadius ? CIRCLE_ROMOVE.INIT : CIRCLE_ROMOVE.IDLE;
        }
        this.CurItem.getComponent(CircleRing).init(this.node, this.CurRadius, this.Width, this.Color);
        return CIRCLE_ROMOVE.CONTINUE;
    }

    //获取比较亮的颜色
    getRandomBrightColor(): Color {
        let r = Math.floor(128 + Math.random() * 128);
        let g = Math.floor(128 + Math.random() * 128);
        let b = Math.floor(128 + Math.random() * 128);
        return new Color(r, g, b, 255);
    }

    onTouchStart(event: EventTouch) {
        this._touchStart = event.getUILocation();
    }

    onTouchEnd(event: EventTouch) {
        // this._touchStart = event.getUILocation();
        const parce: Vec2 = event.getUILocation().subtract(this._touchStart).normalize().multiplyScalar(5);
        this.Player.getComponent(CirclePlayer).velocity = parce;
        this.IsStart = true;
        this.AredNode.active = false;
    }

}


